package bussines;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;

import bussines.logger.Log;

public class FileUtils {

	public static List<File> listFiles(String path, FileFilter ff) {
		File dir = new File(path);
		if (!dir.exists() && !dir.isDirectory()) {
			Log.log("Error.Could not open or it does not exits! " + path);
			throw new IllegalArgumentException(path + " must be a directory !");
		}
		return Arrays.asList(dir.listFiles(ff));
	}

	public static String getExtension(String file) {
		return file.substring(file.lastIndexOf('.'));
	}

	public static String getExtension(File pathname) {
		return getExtension(pathname.getName());
	}

	public static String getNameWithoutExtension(File file) {
		return getNameWithoutExtension(file.getName());
	}

	public static String getNameWithoutExtension(String fileName) {
		return fileName.substring(0, fileName.lastIndexOf('.'));
	}

	/**
	 * Src will be renamed to file2
	 * 
	 * @param src
	 * @param file2
	 */
	public static void renameKeepExtension(File src, File dest) {
		if (src != null && dest != null) {
			try {
				String newName =
				        dest.getParentFile().getAbsolutePath() + '/' + getNameWithoutExtension(src.getName())
				                + getExtension(dest);

				dest.renameTo(new File(newName));
			} catch (Exception e) {
				Log.log(e.getMessage());
			}
		}
	}
}
